package com.sailor.config;

import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 配置中心，具备容错功能，需有默认值垫底
 *
 * @author sailor
 */
@Component
@Slf4j
public class ConfigCenter {

    // 配置在Apollo里
    @Value("${server.down:false}")
    private boolean serverDown;

    public boolean isServerDown() {
        return serverDown;
    }


    /**
     * 更新监听，处理无法直接使用的数据
     *
     * @param changeEvent
     */
    @ApolloConfigChangeListener
    private void updateHandler(ConfigChangeEvent changeEvent) {
        ConfigChange change;
        for (String key : changeEvent.changedKeys()) {
            change = changeEvent.getChange(key);
            log.info("apollo config change - key:{}, oldValue:{}, newValue:{}, changeType:{}", change.getPropertyName(),
                    change.getOldValue(), change.getNewValue(), change.getChangeType());
        }
    }
}
